package com.itboye.sunsun.product.center;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.sunsun.custome.XGridView;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.person.ui.HomeBean;
import com.itboye.sunsun.person.ui.HomeBean.HomeListBean;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.ImageUtil;
import com.itboye.sunsun.utils.XImageLoaderWhiteBg;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;
import com.itboye.sunsunhome.R;

import java.util.ArrayList;

public class ProductCenterHomeActivity extends BaseActivity implements
		OnClickListener {
	private TextView edit_inputcity;
	private ImageView delete;
	private TextView add_shap_title_tv;
	private ImageView close_icon;
	BaseAdapter adapter;
	XGridView listviewxl;

	ArrayList<HomeBean> list = new ArrayList<>();

	@Override
	@SuppressLint("InlinedApi")
	protected void onCreate(Bundle arg0) {
		// 
		super.onCreate(arg0);
		setContentView(R.layout.activity_home_product_center);
		showProgressDialog("正在加载中...", true);
		edit_inputcity.setText(R.string.keyword);
		add_shap_title_tv.setText(R.string.chanpinzhongxin);
		list = new ArrayList<HomeBean>();
		listviewxl = (XGridView) findViewById(R.id.listviewxl);
		
		pullDown();
		close_icon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 
				finish();
			}
		});
		edit_inputcity.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 
				Intent intent = new Intent(ProductCenterHomeActivity.this,
						ProducenterSeaerActivity.class);
				startActivity(intent);
			}
		});
		
		setStatusBarColor(R.color.home_blue);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.delete:
			edit_inputcity.setText("");
			break;
		
		default:
			break;
		}
	}

	private void pullDown() {

		MyJsonRequest.Builder<ArrayList<HomeBean.HomeListBean>> builder = new MyJsonRequest.Builder<ArrayList<HomeBean.HomeListBean>>()
				.apiVer("100")
				.typeKey("BY_ProductCenter_onecate")
				.requestListener(
						new XRequestListener<ArrayList<HomeBean.HomeListBean>>() {

							@Override
							public void onResponse(
									final ArrayList<HomeBean.HomeListBean> orderBean) {

								adapter = new HomeAdapter(
										ProductCenterHomeActivity.this,
										orderBean);
								listviewxl.setAdapter(adapter);
								listviewxl.setOnItemClickListener(new OnItemClickListener() {

									@Override
									public void onItemClick(AdapterView<?> parent, View view,
											int position, long id) {
										// 
										Intent intent=new Intent(ProductCenterHomeActivity.this,ProducenterGridviewActivity.class);
										intent.putExtra("title",orderBean.get(position).getName());
										intent.putExtra("parentid", orderBean.get(position).getId());
										startActivity(intent);
									}
								});
								try {
									closeProgressDialog();
								} catch (Exception e) {
									// TODO: handle exception
								}

							}
						}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
				
					}
				});

		MyJsonRequest<ArrayList<HomeBean.HomeListBean>> request = builder
				.build();
		HttpRequest.getDefaultRequestQueue().add(request);

	}

	class HomeAdapter extends BaseAdapter {
		Context context;
		ArrayList<HomeListBean> bean;

		public HomeAdapter(Context context, ArrayList<HomeListBean> bean) {
			// TODO Auto-generated constructor stub
			this.context = context;
			this.bean = bean;
		}

		@Override
		public int getCount() {
			// 
			return bean == null ? 0 : bean.size();
		}

		@Override
		public Object getItem(int position) {
			// 
			return position;
		}

		@Override
		public long getItemId(int position) {
			// 
			return position;
		}

		@SuppressWarnings("deprecation")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// 
			ViewHouder houder;
			if (convertView == null) {
				houder = new ViewHouder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.item_home_poduceter, null);
				houder.img = (ImageView) convertView.findViewById(R.id.img);
				convertView.setTag(houder);
			} else {
				houder = (ViewHouder) convertView.getTag();
			}
			
	        Drawable drawable = getWallpaper();  
	        //将Drawable转化为Bitmap  
	        Bitmap bitmap = ImageUtil.drawableToBitmap(drawable);  
	        //缩放图片  
	        Bitmap zoomBitmap = ImageUtil.zoomBitmap(bitmap, 100, 100);  
	        //获取圆角图片  
	        Bitmap roundBitmap = ImageUtil.getRoundedCornerBitmap(zoomBitmap, 10.0f);  
	        //获取倒影图片  
	        houder.img.setImageBitmap(roundBitmap);
			XImageLoaderWhiteBg.load(bean.get(position).getIconurl(), houder.img);
			return convertView;
		}

		class ViewHouder {
			
			LinearLayout linyout;
			ImageView img;
			TextView textView1;
		}
	  

	}

}
